package org.caowei.samples.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.caowei.samples.dao.IExamScoreDAO;
import org.caowei.samples.dao.IExamTestDAO;
import org.caowei.samples.dao.ISubjectDAO;
import org.caowei.samples.dao.IUserDAO;
import org.caowei.samples.dao.IUserSubjectDAO;
import org.caowei.samples.model.ExamScore;
import org.caowei.samples.model.ExamTest;
import org.caowei.samples.model.Subject;
import org.caowei.samples.model.User;
import org.caowei.samples.sevice.ExamTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/test")
public class TestController {

	@Autowired
	private IUserDAO iUserDAO;

	@Autowired
	private ExamTestService iExamTestService;

//	@Autowired
//	private IUserSubjectDAO iUserSubjectDAO;

	@Autowired
	private IExamScoreDAO examScoreDAO;

	@Autowired
	private IExamTestDAO examTestDAO;

	@Autowired
	private ISubjectDAO examSubjectDAO;

	@RequestMapping("/toTest")
	public String toTest(String userId, Integer subId, Model model) throws Exception {
		List<ExamTest> testList = iExamTestService.findExamTestsBySubjectId(subId);
		User uId = iUserDAO.getUserById(userId);
		Subject sId = examSubjectDAO.getSubjectById(subId);
		model.addAttribute("User", uId);
		model.addAttribute("testList", testList);
		model.addAttribute("Subject", sId);
		return "student/test";
	}

	@RequestMapping("/testAnswer")
	public String testAnswer(String userId, Integer subId, HttpServletRequest request, HttpSession session, Model model)
			throws Exception {
		List<ExamTest> testlist = examTestDAO.findExamTestBySubjectId(subId);
		Subject SubjectId = examSubjectDAO.getSubjectById(subId);
		//计算成绩
		int score = 0;
		int a = 1;
		for(ExamTest test : testlist){
			String[] Testanswer = request.getParameterValues("answer" + a);
			a ++;
			String answer = null;
			for(String b : Testanswer){
				if(answer == null){
					answer = b;
				}else{
					answer += "," + a;
				}
			}
			
			if(test.getAnswer().equals(answer)){
				score += test.getExamTestType().getTestTypeScore();
			}else{
				score += 0;
			}
		}
		
		ExamScore examScore = new ExamScore();
		examScore.setScore(score);
		examScore.setUserId(userId);
		examScore.setSubjectId(subId);
		Date date =new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd   HH:mm:ss");;
		String TimeString = dateFormat.format(date);
		examScore.setExamTime(date);
		examScoreDAO.save(examScore);
		
		model.addAttribute("Subject", SubjectId);
		model.addAttribute("ExamScore", examScore);
		model.addAttribute("time",TimeString);
		
		
		return "student/EndExamination";
	}

	// public Map<Integer, List<ExamTestVO>> getTestListBySubject(Integer subId,
	// String userId) {
	// // 初始化参数
	// List<ExamTestType> testTypeList = iUserSubjectDAO.loadAll();
	// Map<Integer, Integer> testTypeScoreMap = new HashMap<Integer, Integer>();
	// // 所有题型各自的分数
	// Map<Integer, List<ExamTestVO>> testListMap = new HashMap<Integer,
	// List<ExamTestVO>>();
	// // 获取所有题型，得到testTypeScoreMap
	// if (testTypeList != null) {
	// for (ExamTestType testType : testTypeList) {
	// testTypeScoreMap.put(testType.getId(), testType.getTestTypeScore());
	// // 3.0. 根据科目获取各个题型所有题目
	// List<ExamTest> testList = examTestDAO.getTestListBySubAndTestType(subId,
	// testType.getId());//
	// List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
	// // 分别获取各个题型在数据库里的所有题目
	// for (ExamTest test : testList) {
	// ExamTestVO vo = new ExamTestVO();
	// BeanUtils.copyProperties(test, vo);
	// ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
	// BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
	// vo.setExamTestType(testTypeVO);
	// testVOList.add(vo);
	// }
	// testListMap.put(testType.getId(), testVOList);
	// }
	// } else {
	// return null;
	// }
	// Subject subject = examSubjectDAO.get(subId);
	// if (subject == null) {
	// return null;
	// }
	// Integer totalTestScore = subject.getTotalScore().intValue();
	// Integer totalTestCount = subject.getTestCount();
	// // 1. 校验科目总分和总题数是否匹配
	// if (!TestUtils.checkSubjectScoreAndCount(totalTestCount, totalTestScore,
	// testTypeScoreMap)) {
	// return null;
	// }
	// // 2. 获取各个题型的数量
	// Map<Integer, Integer> testTypeCountMap =
	// TestUtils.getTestCountByScore(totalTestCount, testTypeScoreMap);
	// // 4. 生成随机题目
	// Map<Integer, List<ExamTestVO>> resultTestListMap = new HashMap<Integer,
	// List<ExamTestVO>>();
	// for (Integer key : testTypeCountMap.keySet()) {
	// Integer count = testTypeCountMap.get(key);
	// List<ExamTestVO> testList = testListMap.get(key);
	// // 3.1 在里面
	// List<Integer> randomTestIdList = TestUtils.getRandomTestIdList(testList,
	// count);
	// if (randomTestIdList == null) {
	// return null;
	// }
	// // 根据多个id获取多个题目
	// List<ExamTest> randomTestList =
	// examTestDAO.getTestListByIdList(randomTestIdList);
	// // 转换List<ExamTest> ==>List<ExamTestVO>
	// List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
	// for (ExamTest test : randomTestList) {
	// ExamTestVO vo = new ExamTestVO();
	// BeanUtils.copyProperties(test, vo);
	// ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
	// BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
	// vo.setExamTestType(testTypeVO);
	// testVOList.add(vo);
	// }
	// resultTestListMap.put(key, testVOList);//
	// }
	// ExamScore score = new ExamScore();
	// score.setUserId(userId);
	// score.setSubjectId(subId);
	// score.setExamTime(new Date());
	// // 如果成功取到试题
	// examScoreDAO.save(score);
	// examScoreDAO.flush();
	// return resultTestListMap;
	// }
}
